package com.yuneec.fly.utils;

import android.os.SystemClock;

import com.yuneec.droneservice.parse.Figure6;
import com.yuneec.droneservice.parse.Figure7;
import com.yuneec.droneservice.parse.Figure8;
import com.yuneec.droneservice.utils.ByteUtils;
import com.yuneec.fly.events.EventBusFactory;
import com.yuneec.fly.events.commandEvent.SendCommandEvent;

/**
 * Created by gjw on 2016/12/5.
 */

public class RxHelper {

    public static void sendRx(byte[] datas) {
        LogUtils.e("sendRx Hex     " + ByteUtils.byteArrayToHexString(datas, 0, datas.length));
        Figure7 figure7 = new Figure7();
        figure7.index = 2;
        figure7.receiver_aNum = datas[20];
        figure7.receiver_abits = datas[21];
        figure7.receiver_trNum = datas[22];
        figure7.receiver_trbits = datas[23];
        figure7.receiver_swNum = datas[24];
        figure7.receiver_swbits = datas[25];
        figure7.receiver_reply_channel_num = datas[26];
        figure7.receiver_reply_channel_bits = datas[27];
        //first request all the monitor data rx's got
        figure7.transmitter_request_channel_num = datas[26];
        figure7.transmitter_request_channel_bits = datas[27];

        figure7.extra_channel_num = datas[28];
        figure7.extra_channel_bits = datas[29];

        sendAnalogFigure8(figure7, datas);
        figure7.receiver_analog_parse_type = 6;
        SystemClock.sleep(60);

        sendTrimFigure8(figure7, datas);
        figure7.receiver_trim_parse_type = 7;
        SystemClock.sleep(60);

        sendSwitchFigure8(figure7, datas);
        figure7.receiver_switch_parse_type = 8;
        SystemClock.sleep(60);

        sendMonitorFigure8(figure7, datas);
        figure7.receiver_reply_channel_parse_type = 9;
        figure7.transmitter_request_channel_parse_type = 9;
        SystemClock.sleep(60);

        boolean send = sendExtraFigure8(figure7, datas);
        figure7.extra_channel_parse_type = (byte) (send ? 10 : 9);
        SystemClock.sleep(60);

//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure7.getDatas()),null));
        SystemClock.sleep(60);

        Figure6 figure6 = new Figure6();
        figure6.index = 0;
        figure6.receiver_type = new byte[]{datas[6], datas[7]};
        figure6.pan_id = new byte[]{datas[8], datas[9]};
        figure6.receiver_addr = new byte[]{datas[10], datas[11]};
        figure6.receiver_addr = new byte[]{datas[10], datas[11]};
        figure6.parse_index = 2;
        figure6.tx_addr = new byte[]{datas[datas.length - 2], datas[datas.length - 1]};
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure6.getDatas()),null));
    }

    private static void sendAnalogFigure8(Figure7 figure7, byte[] datas) {
        Figure8 figure8 = new Figure8();
        figure8.index = 6;
        byte[] channels = new byte[figure7.receiver_aNum & 0xff];
        for (int i = 0; i < channels.length; i++) {
            channels[i] = datas[30 + i];
        }
        figure8.channel_map = channels;
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure8.getDatas()),null));
    }

    private static void sendTrimFigure8(Figure7 figure7, byte[] datas) {
        Figure8 figure8 = new Figure8();
        figure8.index = 7;
        byte[] channels = new byte[figure7.receiver_trNum & 0xff];
        for (int i = 0; i < channels.length; i++) {
            channels[i] = datas[30 + figure7.receiver_aNum + i];
        }
        figure8.channel_map = channels;
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure8.getDatas()),null));
    }

    private static void sendSwitchFigure8(Figure7 figure7, byte[] datas) {
        Figure8 figure8 = new Figure8();
        figure8.index = 8;
        byte[] channels = new byte[figure7.receiver_swNum & 0xff];
        for (int i = 0; i < channels.length; i++) {
            channels[i] = datas[30 + figure7.receiver_aNum + figure7.receiver_trNum + i];
        }
        figure8.channel_map = channels;
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure8.getDatas()),null));
    }

    private static void sendMonitorFigure8(Figure7 figure7, byte[] datas) {
        if ((figure7.receiver_reply_channel_num & 0xff) == 0) {
            return;
        }
        Figure8 figure8 = new Figure8();
        figure8.index = 9;
        byte[] channels = new byte[figure7.receiver_reply_channel_num & 0xff];
        for (int i = 0; i < channels.length; i++) {
            channels[i] = datas[30 + figure7.receiver_aNum + figure7.receiver_trNum + figure7.receiver_swNum + i];
        }
        figure8.channel_map = channels;
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure8.getDatas()),null));
    }

    private static boolean sendExtraFigure8(Figure7 figure7, byte[] datas) {
        if ((figure7.extra_channel_num & 0xff) == 0) {
            return false;
        }
        Figure8 figure8 = new Figure8();
        figure8.index = 10;
        byte[] channels = new byte[figure7.extra_channel_num & 0xff];
        for (int i = 0; i < channels.length; i++) {
            channels[i] = datas[30 + figure7.receiver_aNum + figure7.receiver_trNum + figure7.receiver_swNum + figure7.receiver_reply_channel_num + i];
        }
        figure8.channel_map = channels;
//        EventBusFactory.getCommandBus().post(new SendCommandEvent(new SendRxResInfo(figure8.getDatas()),null));
        return true;
    }
}
